<?php

/**
 * @author Ermek
 * @copyright Hire-Experts LLC
 * @version Avatar 1.01
 */

class he_avatar
{
	function upload_photo( $photo_name )
	{
		global $user, $database, $url;

		if ( !$user->user_exists ) {
			return false;
		}
		
		// ENSURE USER DIRECTORY IS ADDED
		$user_directory = $url->url_userdir($user->user_info['user_id']);
		$user_path_array = explode("/", $user_directory);
		array_pop($user_path_array);
		array_pop($user_path_array);
		$subdir = implode("/", $user_path_array) . "/";
		
		if( !is_dir($subdir) )
		{
			mkdir($subdir, 0777); 
			chmod($subdir, 0777); 
			$handle = fopen($subdir . "index.php", 'x+');
			fclose($handle);
		}
		if( !is_dir($user_directory) )
		{
			mkdir($user_directory, 0777);
			chmod($user_directory, 0777);
			$handle = fopen($user_directory . "/index.php", 'x+');
			fclose($handle);
		}

		// SET KEY VARIABLES
		$file_maxsize = "4194304";
		$file_exts = explode(",", str_replace(" ", "", strtolower($user->level_info['level_photo_exts'])));
		$file_types = explode(",", str_replace(" ", "", strtolower("image/jpeg, image/jpg, image/jpe, image/pjpeg, image/pjpg, image/x-jpeg, x-jpg, image/gif, image/x-gif, image/png, image/x-png")));
		$file_maxwidth = 800;
		$file_maxheight = 800;
		
		$photo_newname = "he_avatar.jpg";
		$file_dest = $url->url_userdir($user->user_info['user_id']) . $photo_newname;

		$new_photo = new se_upload();
		$new_photo->new_upload($photo_name, $file_maxsize, $file_exts, $file_types, $file_maxwidth, $file_maxheight);

		// UPLOAD AND RESIZE PHOTO IF NO ERROR
		if( !$new_photo->is_error )
		{
			// CHECK IF IMAGE RESIZING IS AVAILABLE, OTHERWISE MOVE UPLOADED IMAGE
			if( $new_photo->is_image )
			{
				$new_photo->upload_photo($file_dest);
			}
						
			return true;
		}
		else
		{
			return false;
		}
	}
	
	function crop_photo( $crop_area )
	{
		global $user, $database, $url, $actions;
		
		if ( !$user->user_exists ) {
			return false;
		}
		
		$file_name =  $url->url_userdir($user->user_info['user_id']) . "he_avatar.jpg";
		
		$photo_newname = "0_" . rand(1000, 9999) . ".jpg";
		$file_dest = $url->url_userdir($user->user_info['user_id']) . $photo_newname;
		$thumb_dest = substr($file_dest, 0, strrpos($file_dest, ".")) . "_thumb" . substr($file_dest, strrpos($file_dest, "."));

		$file_maxwidth = $user->level_info['level_photo_width'];
		$file_maxheight = $user->level_info['level_photo_height'];


		$file = imagecreatetruecolor($file_maxwidth, $file_maxheight);
		$new = imagecreatefromjpeg($file_name);
		
		for ( $i=0; $i<256; $i++ ) {
			imagecolorallocate($file, $i, $i, $i);
		}
		
		imagecopyresampled($file, $new, 0, 0, $crop_area['left'], $crop_area['top'], $file_maxwidth, $file_maxheight, $crop_area['width'], $crop_area['height']);
		imagejpeg($file, $file_dest, 100);
		ImageDestroy($new);
		ImageDestroy($file);
		
		chmod($file_dest, 0777);
		
		$file = imagecreatetruecolor(60, 60);
		$new = imagecreatefromjpeg($file_name);
		
		for ( $i=0; $i<256; $i++ ) {
			imagecolorallocate($file, $i, $i, $i);
		}
		
		imagecopyresampled($file, $new, 0, 0, $crop_area['left'], $crop_area['top'], 60, 60, $crop_area['width'], $crop_area['height']);
		imagejpeg($file, $thumb_dest, 100);
		ImageDestroy($new);
		ImageDestroy($file);
		
		chmod($thumb_dest, 0777);
		
		//@unlink($file_name);
		
		$user->user_photo_delete();
		
		$database->database_query( "UPDATE se_users SET user_photo='{$photo_newname}' WHERE user_id='{$user->user_info['user_id']}' LIMIT 1" );
		$user->user_info['user_photo'] = $photo_newname;
		
		// INSERT ACTION
		$action_media = array( array( 'media_link'=>$url->url_create('profile', $user->user_info['user_username']), 'media_path'=>$user->user_photo(), 'media_width'=>100, 'media_height'=>100 ) );
		$actions->actions_add($user, "editphoto", array($user->user_info['user_username'], $user->user_displayname), $action_media, 999999999, TRUE, "user", $user->user_info['user_id'], $user->user_info['user_privacy']);		
	}

	function avatar_list()
	{
		$query = he_database::placeholder( "SELECT * FROM `se_he_avatar` WHERE 1 ORDER BY `id`" );
		
		return he_database::fetch_array($query);
	}
	
	function get_avatar_src( $avatar_id )
	{
		if ( !$avatar_id )
		{
			return false;
		}
		
		$query = he_database::placeholder( "SELECT `filename` FROM `se_he_avatar`
			WHERE `id`=?", $avatar_id );
		
		return he_database::fetch_field($query);
	}
	
	function save_photo()
	{
		global $user, $database, $url, $actions;
		
		$avatar_src = $url->url_userdir($user->user_info['user_id']) . 'he_avatar.jpg';
		
		if ( !file_exists($avatar_src) )
		{
			return false;
		}
		
		$photo_newname = "0_" . rand(1000, 9999) . ".jpg";
		$file_dest = $url->url_userdir($user->user_info['user_id']) . $photo_newname;
		$thumb_dest = substr($file_dest, 0, strrpos($file_dest, ".")) . "_thumb" . substr($file_dest, strrpos($file_dest, "."));
		
		copy($avatar_src, $file_dest);
		chmod($file_dest, 0777);
		
		copy($avatar_src, $thumb_dest);
		chmod($thumb_dest, 0777);
		
		$user->user_photo_delete();
		
		$database->database_query( "UPDATE se_users SET user_photo='{$photo_newname}' WHERE user_id='{$user->user_info['user_id']}' LIMIT 1" );
		$user->user_info['user_photo'] = $photo_newname;
		
		// INSERT ACTION
		$action_media = array( array( 'media_link'=>$url->url_create('profile', $user->user_info['user_username']), 'media_path'=>$user->user_photo(), 'media_width'=>100, 'media_height'=>100 ) );
		$actions->actions_add($user, "editphoto", array($user->user_info['user_username'], $user->user_displayname), $action_media, 999999999, TRUE, "user", $user->user_info['user_id'], $user->user_info['user_privacy']);	
	}
	
	function choose_avatar( $avatar_id )
	{		
		if ( !$avatar_id )
		{
			return false;
		}
		
		global $user, $database, $url, $actions;

		$avatar_src = he_avatar::get_avatar_src($avatar_id);
		
		if ( !$user->user_exists || !$avatar_src ) {
			return false;
		}
		
		// ENSURE USER DIRECTORY IS ADDED
		$user_directory = $url->url_userdir($user->user_info['user_id']);
		$user_path_array = explode("/", $user_directory);
		array_pop($user_path_array);
		array_pop($user_path_array);
		$subdir = implode("/", $user_path_array) . "/";
		
		if( !is_dir($subdir) )
		{
			mkdir($subdir, 0777); 
			chmod($subdir, 0777); 
			$handle = fopen($subdir . "index.php", 'x+');
			fclose($handle);
		}
		if( !is_dir($user_directory) )
		{
			mkdir($user_directory, 0777);
			chmod($user_directory, 0777);
			$handle = fopen($user_directory . "/index.php", 'x+');
			fclose($handle);
		}

		$photo_newname = "0_" . rand(1000, 9999) . ".jpg";
		$file_dest = $url->url_userdir($user->user_info['user_id']) . $photo_newname;
		$thumb_dest = substr($file_dest, 0, strrpos($file_dest, ".")) . "_thumb" . substr($file_dest, strrpos($file_dest, "."));
		
		$avatar_src = "./uploads_admin/avatar/$avatar_src";
		
		copy($avatar_src, $file_dest);
		chmod($file_dest, 0777);
		
		copy($avatar_src, $thumb_dest);
		chmod($thumb_dest, 0777);
		
		$user->user_photo_delete();
		
		$database->database_query( "UPDATE se_users SET user_photo='{$photo_newname}' WHERE user_id='{$user->user_info['user_id']}' LIMIT 1" );
		$user->user_info['user_photo'] = $photo_newname;
		
		// INSERT ACTION
		$action_media = array( array( 'media_link'=>$url->url_create('profile', $user->user_info['user_username']), 'media_path'=>$user->user_photo(), 'media_width'=>100, 'media_height'=>100 ) );
		$actions->actions_add($user, "editphoto", array($user->user_info['user_username'], $user->user_displayname), $action_media, 999999999, TRUE, "user", $user->user_info['user_id'], $user->user_info['user_privacy']);	
	}
	
	function delete_avatar( $avatar_id )
	{
		$avatar_src = he_avatar::get_avatar_src($avatar_id);
		
		if ( !$avatar_src )
		{
			return false;
		}
		
		$avatar_src = "../uploads_admin/avatar/$avatar_src";		
		@unlink($avatar_src);
		
		$query = he_database::placeholder( "DELETE FROM `se_he_avatar` 
			WHERE `id`=?", $avatar_id );
		
		he_database::query( $query );
		
		return he_database::affected_rows();
	}
	
	function new_avatar_id()
	{
		$query = "SELECT MAX(`id`) FROM `se_he_avatar` WHERE 1";
		$last_id = (int)he_database::fetch_field($query);

		return $last_id + 1;
	}
	
	function add_avatar( $avatar_id, $filename )
	{
		if ( !$avatar_id || !$filename )
		{
			return false;
		}
		
		$query = he_database::placeholder( "INSERT INTO `se_he_avatar` (`id`, `filename`) VALUES(?, '?')",
			$avatar_id, $filename );
		
		he_database::query($query);		
	}
}
?>